#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"ui_modifyandcreate.h"
#include "showwidget.h"
#include "clickqlabel.h"
#include"modifyandcreate.h"
#include<QtDebug>
#include<QMessageBox>
#include<QSqlError>
#include<QDialog>
#include<QDialogButtonBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->SetStyle();
    this->setWindowIcon(QIcon(":User.jpeg"));
    modifyAndCreate = new ModifyAndCreate();
    connect(ui->connect_button,&QPushButton::clicked,this,[=](){
        QString passWord = ui->password->text();
        userName = ui->User_name->text();
        if(userName==NULL)
        {
            ui->label_3->setVisible(true);
            return;
        }
        else
        {
            ui->label_3->setVisible(false);
        }
        if(passWord==NULL)
        {
            ui->label_4->setText("密码为空");
            ui->label_4->setVisible(true);
        }
        else
        {
            ui->label_4->setVisible(false);
        }
        passWord = ui->password->text();
        qDebug()<<passWord;
        this->ConnectDatabase(userName,passWord);
    });
    connect(ui->exit_button,&QPushButton::clicked,this,&MainWindow::Login);
    addConnect();


}





void MainWindow::ConnectDatabase(QString userName,QString passWord)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setDatabaseName("carlibrary");
//    db.setHostName("l");
    db.setUserName(userName);//userName.toUtf8().data()
    db.setPassword(passWord);//passWord.toUtf8().data()
    db.setPort(3306);
    bool ok = db.open();
    if(ok)
    {
        msgBox = new QMessageBox(this);
        msgBox->setWindowTitle("提示");
        msgBox->setText("连接成功");
        msgBox->setStandardButtons(QMessageBox::Ok);
        msgBox->exec();
        this->hide();
        SHowWidget();
    }
    else
    {
        if(db.lastError().text()==QString("Access denied for user '%1'@'localhost' (using password: YES) QMYSQL: Unable to connect").arg(userName))
            {
                ui->password->clear();
                ui->label_4->setText("密码错误");
                ui->label_4->setVisible(true);
                ui->label_3->setVisible(false);
            }
        qDebug()<<db.lastError().text();

     }
}

void MainWindow::Login()
{
    QMessageBox msgBox(this);
    msgBox.setWindowTitle("确认");
    msgBox.setText("确认要退出么");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Cancel);
    int res = msgBox.exec();
    if(res ==QMessageBox::Ok)
    {
        exit(1);
    }
    else
    {
        msgBox.close();
    }
}
void MainWindow::SetStyle()
{
    ui->label_3->setVisible(false);
    ui->label_3->setStyleSheet("color:red");
    ui->label_4->setVisible(false);
    ui->label_4->setStyleSheet("color:red");
}
void MainWindow::closeEvent(QCloseEvent *event)
{
    int r = QMessageBox::warning(this, tr("X"),
                                     tr("确定要关闭窗口么 "
                                        ""),
                                     QMessageBox::Yes
                                     | QMessageBox::Cancel);
        if (r == QMessageBox::Yes) {
            exit(1);
        }
        else
        {
            event->ignore();
        }

}
void MainWindow::SHowWidget()
{
    ShowWidget* widget = new ShowWidget(nullptr,this->userName) ;
    widget->show();
}
void MainWindow::addConnect(){
    connect(ui->label_5,&ClickQLabel::click,this,[=](){

        modifyAndCreate->setWindowTitle("注册用户");
        modifyAndCreate->ui->user->setText("用户名");
        modifyAndCreate->ui->oldPassword->setVisible(false);
        modifyAndCreate->ui->oldPassword_line->setVisible(false);
        modifyAndCreate->ui->user_line->setPlaceholderText("请输入用户名");
        Register();
        modifyAndCreate->exec();
    });
    connect(ui->label_6,&ClickQLabel::click,this,[=](){

        modifyAndCreate->setWindowTitle("修改密码");
        modifyAndCreate->ui->oldPassword->setText("旧密码");
        modifyAndCreate->ui->user->setText("用户名");
        modifyAndCreate->ui->oldPassword->setVisible(true);
        modifyAndCreate->ui->oldPassword_line->setVisible(true);
        modifyAndCreate->ui->user_line->setPlaceholderText("请输入用户名");
        modify();
        modifyAndCreate->exec();
    });


}
void MainWindow::Register()
{
       disconnect(modifyAndCreate->ui->pushButton,&QPushButton::clicked,0,0);
       connect(modifyAndCreate->ui->pushButton,&QPushButton::clicked,modifyAndCreate,[=](){
           QString password = modifyAndCreate->ui->password_line->text();
           QString password2 = modifyAndCreate->ui->password_line2->text();
           if(password != password2)
           {
                modifyAndCreate->ui->label->setText("两次输入的密码不一致");
                modifyAndCreate->ui->password_line->clear();
                modifyAndCreate->ui->password_line2->clear();
           }
           else
           {
               QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
               db.setHostName("localhost");
               db.setPort(3306);
//               db.setDatabaseName("carlibrary");
               db.setUserName("root");
               db.setPassword("root");
               bool ok = db.open();
               if(ok)
               {
                   QString sql = QString("create user '%1'@'%' identified with mysql_native_password by '%2';").arg(modifyAndCreate->ui->user_line->text()).arg(modifyAndCreate->ui->password_line->text());
                   QSqlQuery* query = new QSqlQuery();
                   query->prepare(sql);
                   ok = query->exec();
                   if(ok)
                   {
                       sql = QString("grant select,insert on carlibrary.停车 to '%1'@'%';").arg(modifyAndCreate->ui->user_line->text());
                       query->exec(sql);
                       sql = QString("grant select,insert on carlibrary.开出 to '%1'@'%';").arg(modifyAndCreate->ui->user_line->text());
                       query->exec(sql);
                       sql = QString("grant select,insert on carlibrary.预定 to '%1'@'%';").arg(modifyAndCreate->ui->user_line->text());
                       query->exec(sql);
                       sql = QString("grant select on carlibrary.车位 to '%1'@'%';").arg(modifyAndCreate->ui->user_line->text());
                       bool isok = query->exec(sql);
                       qDebug()<<isok;
                       qDebug()<<"创建用户成功";
                       msgBox = new QMessageBox(this);
                       msgBox->setWindowTitle("提示");
                       msgBox->setText("创建成功");
                       msgBox->setStandardButtons(QMessageBox::Ok);
                       msgBox->exec();
                       modifyAndCreate->close();
                       db.close();
                   }
                   else
                   {
                       qDebug()<<"创建用户失败";
                       msgBox = new QMessageBox(this);
                       msgBox->setWindowTitle("提示");
                       msgBox->setText("创建失败" + query->lastError().text());
                       msgBox->setStandardButtons(QMessageBox::Ok);
                       msgBox->exec();
                   }
               }
    }
       });
}
void MainWindow::modify()
{
        disconnect(modifyAndCreate->ui->pushButton,&QPushButton::clicked,0,0);
        connect(modifyAndCreate->ui->pushButton,&QPushButton::clicked,modifyAndCreate,[=](){
            QString password = modifyAndCreate->ui->password_line->text();
            QString password2 = modifyAndCreate->ui->password_line2->text();
            if(password != password2)
            {
                 modifyAndCreate->ui->label->setText("两次输入的密码不一致");
                 modifyAndCreate->ui->password_line->clear();
                 modifyAndCreate->ui->password_line2->clear();
            }
            else
            {
                QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                db.setPort(3306);
//                db.setDatabaseName("carlibrary");
                db.setUserName(modifyAndCreate->ui->user_line->text().toUtf8().data());
                qDebug()<<modifyAndCreate->ui->user_line->text();
                db.setPassword(modifyAndCreate->ui->oldPassword_line->text().toUtf8().data());
                qDebug()<<modifyAndCreate->ui->oldPassword_line->text();
                bool ok = db.open();
                if(ok)
                {

                    QString sql = QString("set password for '%1'@'%' ='%2'").arg(modifyAndCreate->ui->user_line->text()).arg(modifyAndCreate->ui->password_line->text());
                    qDebug()<<sql;
                    QSqlQuery* query = new QSqlQuery();
                    query->prepare(sql);
                    bool ok = query->exec();
                    if(ok)
                    {
                        msgBox = new QMessageBox(this);
                        msgBox->setWindowTitle("提示");
                        msgBox->setText("修改密码成功");
                        msgBox->setStandardButtons(QMessageBox::Ok);
                        msgBox->exec();
                        modifyAndCreate->close();
                        db.close();
                    }
                    else
                    {
                        msgBox = new QMessageBox(this);
                        msgBox->setWindowTitle("提示");
                        msgBox->setText("修改失败" + query->lastError().text());
                        msgBox->setStandardButtons(QMessageBox::Ok);
                        msgBox->exec();
                    }
                }
                else
                {
                    msgBox = new QMessageBox(this);
                    msgBox->setWindowTitle("提示");
                    msgBox->setText("用户名或者密码错误"+ query.lastError().text());
                    modifyAndCreate->ui->user_line->clear();
                    modifyAndCreate->ui->oldPassword_line->clear();
                    modifyAndCreate->ui->password_line->clear();
                    modifyAndCreate->ui->password_line2->clear();
                    msgBox->setStandardButtons(QMessageBox::Ok);
                    msgBox->exec();
                }
            }

    });


}
MainWindow::~MainWindow()
{
    delete ui;
}

